/**  
* @Title: ShopTablesController.java
* @Package com.lemon.controller.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年11月2日
* @version V1.0  
*/
package com.lemon.controller.cityRun;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
// import java.util.zip.ZipEntry;
// import java.util.zip.ZipOutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lemon.conf.MyException;
import com.lemon.conf.MyInterceptor.LoginRequired;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.model.cityRun.CrShopTables;
import com.lemon.model.cityRun.CrShopTablesServer;
import com.lemon.service.cityRun.ShopTablesService;
import com.lemon.utils.CityRunUtils;
import com.lemon.utils.ReqResult;
import com.lemon.utils.StringUtil;
import com.lemon.utils.file.FileUtils;

import net.sf.json.JSONException;

/**
 * <p>
 * Title: ShopTablesController
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author Ran_cc
 * @date 2018年11月2日
 * @版本 V 1.0
 */
@RestController
@RequestMapping("/table")
public class ShopTablesController {

	@Resource
	private ShopTablesService tabService;

	////////////////呼叫服务/////////////////
	@RequestMapping("/getServerList")
	public ReqResult getOrdersList(@ModelAttribute CrShopTablesServer server , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			//			server.setPageNum(500);
			server.setWhere(" and a.status != 0  and a.create_time >  ? " , CityRunUtils.getBusineDateString());
			server.setOrderBy(" order by a.create_time desc");
			List<CrShopTablesServer> list = tabService.selectServerListBySelective(server);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	///////////////////////桌台相关/////////////////////
	/**
	 * <b>Description:<blockquote> 查询桌台列表信息
	 * </blockquote></b>
	 * 
	 * @Title: getTablesList
	 * @param tab
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月2日
	 */
	@RequestMapping("/getTablesList")
	public ReqResult getTablesList(@ModelAttribute CrShopTables tab , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			tab.setWhere(" and a.status != 0 ");
			tab.setOrderBy(" order by a.create_time desc");
			List<CrShopTables> list = tabService.selectListBySelective(tab);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 根据id查询桌台信息
	 * </blockquote></b>
	 * 
	 * @Title: getTablesById
	 * @param tab
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月2日
	 */
	@RequestMapping("/getTablesById")
	public ReqResult getTablesById(@ModelAttribute CrShopTables tab , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			List<CrShopTables> list = tabService.selectListBySelective(tab);
			reqResult.setReturnResult(list.size() > 0 ? list.get(0) : null);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 修改桌台信息
	 * </blockquote></b>
	 * 
	 * @Title: updateTablesInfo
	 * @param tab
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月2日
	 */
	@LoginRequired
	@RequestMapping("/updateTablesInfo.do")
	public ReqResult updateTablesInfo(@ModelAttribute CrShopTables tab , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			String ids = request.getParameter("ids");
			if (StringUtil.isNotBlank(ids)) {
				String[] split = ids.split(",");
				for (int i = 0; i < split.length; i++) {
					String id = split[i];
					tab.setStatus(0);
					tab.setId(id);
					tabService.updateBySelective(tab);
				}
				reqResult.setReturnResult("1");
				return reqResult;
			}

			int row = tabService.updateBySelective(tab);
			reqResult.setReturnResult(row);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 修改桌台信息
	 * </blockquote></b>
	 * 
	 * @Title: updateTablesInfo
	 * @param tab
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月2日
	 */
	@LoginRequired
	@RequestMapping("/addTablesInfo.do")
	public ReqResult addTablesInfo(@ModelAttribute CrShopTables tab , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			tab.setStatus(1);
			int row = tabService.insertBySelective(tab);
			reqResult.setReturnResult(row);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 批量下载桌台二维码
	 * </blockquote></b>
	 * 
	 * @Title: updateTablesInfo
	 * @param tab
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月2日
	 */
	@RequestMapping("/downQrBatch")
	public void downQrBatch(@ModelAttribute CrShopTables tab , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			String ids = request.getParameter("ids");
			String[] split = ids.split(",");
			String idss = "";
			for (int i = 0; i < split.length; i++) {
				idss += IdTypeHandler.decode(split[i]);
				if ((i + 1) < split.length) {
					idss += ",";
				}
			}
			tab.setWhere(" and  a.id in (" + idss + ")");
			List<CrShopTables> list = tabService.selectListBySelective(tab);
			File file = new File("桌台二维码.zip");
			if (!file.exists()) {
				file.createNewFile();
			}
			response.reset();
			// response.getWriter()
			// 创建文件输出流
			FileOutputStream fous = new FileOutputStream(file);
			// 打包的方法我们会用到ZipOutputStream这样一个输出流, 所以这里我们把输出流转换一下
			ZipOutputStream zipOut = new ZipOutputStream(fous);
			for (CrShopTables aa : list) {
				try {
					String fileUrl = aa.getQrcodePic();
					String fileName = aa.getTitle() + fileUrl.substring(fileUrl.lastIndexOf(".") , fileUrl.length()); // 获取远程文件的文件名。
					// iso-8859-1
					ZipEntry entry = new ZipEntry(fileName);
					zipOut.putNextEntry(entry);
					zipOut.setEncoding("GBK");
					URL urlfile = null;
					urlfile = new URL(fileUrl);
					HttpURLConnection httpUrl = null;
					httpUrl = (HttpURLConnection) urlfile.openConnection();
					httpUrl.connect();
					InputStream downloadFile = httpUrl.getInputStream();
					int len = 0;
					byte[] buf = new byte[1024];
					while ((len = downloadFile.read(buf , 0 , 1024)) != -1) {
						zipOut.write(buf , 0 , len);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
			zipOut.close();
			fous.close();
			FileUtils.downloadZip(file , response);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
	}

	/**
	 * <b>Description:<blockquote> 下载桌台二维码
	 * </blockquote></b>
	 * 
	 * @Title: updateTablesInfo
	 * @param tab
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月2日
	 */
	@RequestMapping("/downQr")
	public void downQr(@ModelAttribute CrShopTables tab , HttpServletRequest request , HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			List<CrShopTables> list = tabService.selectListBySelective(tab);
			if (list.size() > 0) {
				String url = list.get(0).getQrcodePic();
				String fileName = list.get(0).getTitle() + url.substring(url.lastIndexOf(".") , url.length());
				//String excelPath = request.getSession().getServletContext().getRealPath("/Excel/"+"荒.xls");
				FileUtils.downloadPicture( url, fileName , response);
			}
			//获取文件的路径
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
	}

}
